import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import './assets/font/iconfont.css'
import 'amfe-flexible'
import {validateTokenUrls} from './assets/js/url'
import { Button,Tabbar, TabbarItem,Search,Toast,NavBar,Image as VanImage,Lazyload,Stepper,GoodsAction, GoodsActionIcon, GoodsActionButton,Form,Field,Swipe, SwipeItem,Checkbox, CheckboxGroup,Area,Popup,Cell, CellGroup,Switch,Icon,RadioGroup, Radio,Uploader } from 'vant';
Vue.use(VueAxios, axios)
Vue.use(Button);
Vue.use(Tabbar).use(TabbarItem);
Vue.use(Search);
Vue.use(Toast);
Vue.use(NavBar);  
Vue.use(VanImage);  
Vue.use(Lazyload);  
Vue.use(Stepper);  
Vue.use(Icon);  
Vue.use(Uploader);  
Vue.use(RadioGroup).use(Radio);  
Vue.use(Cell).use(CellGroup);  
Vue.use(Area).use(Popup);  
Vue.use(Form).use(Field);  
Vue.use(Swipe).use(SwipeItem).use(Lazyload);
Vue.use(GoodsAction).use(GoodsActionButton).use(GoodsActionIcon);  
Vue.use(Checkbox).use(CheckboxGroup).use(Switch);
Vue.config.productionTip = false
// 设请求基础路径
axios.defaults.baseURL='http://www.kangliuyong.com:10002'

// axios拦截器
// 添加请求拦截器
axios.interceptors.request.use(config=>{
  // 在发送请求之前做些什么
  Toast.loading({
    message: '加载中...',
    forbidClick: true,
  });
  console.log('config==>',config);
 
  // 
  let appkey='U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';
  let token=sessionStorage.getItem('token')
  if(config.method==='post'){
    console.log('post请求');
      // 进行post请求参数序列化
      config.data = config.data || {};
      let dataString = '';
      for(let key in config.data){
        dataString +=`${key}=${config.data[key]}&`
      }
      config.data = dataString + 'appkey=' +appkey;
      // 在url.js里找到则需要验证token
      if(validateTokenUrls.indexOf(config.url)>-1){
        config.data += `&tokenString=${token}`
        // console.log('config.data',config.data);
      }

  }else if(config.method==='get'){
        config.params=config.params || {};
        config.params.appkey = appkey;
         // 找到则需要验证token
      if(validateTokenUrls.indexOf(config.url)>-1){
        config.params.tokenString=token;
      }
  }
  return config;
},error=> {
  // 对请求错误做些什么
  Toast.clear()
  return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(response=>{
  // 对响应数据做点什么
  Toast.clear()
  return response;
},error=> {
  // 对响应错误做点什么
  Toast.clear()
  return Promise.reject(error);
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
